import type { ShowLoadingOptions, ShowModalOptions, ShowToastOptions } from './feedback-types.ts';

/**
 * 显示Toast提示
 * @param message 提示消息
 * @param options 可选配置项
 */
export function showToast(message: string, options: Partial<ShowToastOptions> = {}): Promise<void> {
  const {
    type = 'none',
    duration = 2000,
    icon = 'none',
    mask = true,
    position = 'center',
  } = options;

  return new Promise((resolve) => {
    uni.showToast({
      title: message,
      icon: type === 'success'
        ? 'success'
        : type === 'error' ? 'error' : 'none',
      image: options.image,
      duration,
      mask,
      position,
      success: () => {
        setTimeout(resolve, duration);
      },
      fail: (err) => {
        console.error('showToast error:', err);
        resolve();
      },
    });
  });
}

/**
 * 显示成功Toast
 * @param message 成功消息
 * @param duration 显示时长(ms)
 */
export function showSuccessToast(message: string, duration = 1500) {
  return showToast(message, { type: 'success', duration });
}

/**
 * 显示错误Toast
 * @param message 错误消息
 * @param duration 显示时长(ms)
 */
export function showErrorToast(message: string, duration = 2000) {
  return showToast(message, { type: 'error', duration });
}

/**
 * 显示加载提示
 * @param title 加载提示文本
 * @param options 可选配置项
 */
export function showLoading(title = '加载中...', options: Partial<ShowLoadingOptions> = {}): void {
  const { mask = true, delay = 0 } = options;

  if (delay > 0) {
    setTimeout(() => {
      uni.showLoading({
        title,
        mask,
        success: options.onShow,
        fail: options.onError,
      });
    }, delay);
  } else {
    uni.showLoading({
      title,
      mask,
      success: options.onShow,
      fail: options.onError,
    });
  }
}

/**
 * 隐藏加载提示
 */
export function hideLoading(): void {
  uni.hideLoading();
}

/**
 * 显示模态对话框
 * @param options 对话框配置
 */
export function showModal(options: ShowModalOptions): Promise<{ confirm: boolean, cancel: boolean }> {
  const {
    title = '提示',
    content = '',
    showCancel = true,
    confirmText = '确定',
    cancelText = '取消',
    confirmColor = '#576B95',
    cancelColor = '#999999',
  } = options;

  return new Promise((resolve) => {
    uni.showModal({
      title,
      content,
      showCancel,
      confirmText,
      cancelText,
      confirmColor,
      cancelColor,
      success: (res) => {
        resolve({
          confirm: res.confirm,
          cancel: res.cancel,
        });
      },
      fail: (err) => {
        console.error('showModal error:', err);
        resolve({
          confirm: false,
          cancel: true,
        });
      },
    });
  });
}

/**
 * 显示简易确认对话框
 * @param content 内容文本
 * @param title 标题
 */
export function showConfirm(content: string, title = '提示'): Promise<boolean> {
  return showModal({
    title,
    content,
    showCancel: true,
  }).then(res => res.confirm);
}

/**
 * 显示带输入的对话框
 * @param options 对话框配置
 */
export function showPrompt(options: {
  title?: string
  placeholder?: string
  defaultValue?: string
  confirmText?: string
} = {}): Promise<string | null> {
  return new Promise((resolve) => {
    // 微信小程序不支持直接输入对话框，需要自定义实现
    // 这里使用uni-app的showModal模拟基础功能
    showModal({
      title: options.title || '请输入',
      content: options.placeholder || '',
      confirmText: options.confirmText || '确定',
      showCancel: true,
    }).then((res) => {
      // 在真实项目中，这里应该使用自定义组件实现输入功能
      resolve(res.confirm ? options.defaultValue || '' : null);
    });
  });
}

/**
 * 自动管理的Loading调用
 * @param action 需要Loading的异步操作
 * @param options Loading配置
 */
export async function withLoading<T>(action: () => Promise<T>, options: {
  title?: string
  onError?: (error: any) => void
} = {}): Promise<T | null> {
  showLoading(options.title || '处理中...');
  try {
    const result = await action();
    hideLoading();
    return result;
  } catch (error) {
    hideLoading();
    if (options.onError) {
      options.onError(error);
    } else {
      showErrorToast(typeof error === 'string' ? error : '操作失败');
    }
    return null;
  }
}
